﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class BgmRequestExpenseDAL : IDALBgmRequestExpense
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public BgmRequestExpenseDAL()
        {

        }

        public BgmRequestExpenseInfo GetBgmRequestExpense(int seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            BgmRequestExpenseInfo info = new BgmRequestExpenseInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT re.*"
                            + ", ex.BGM_EXP_NAME"
                            + ", ri.ICM_RES_ITEM_NAME"
                            + ", u.ICM_UNIT_NAME"
                            + ", s.BGM_SOURCE_NAME"
                        + " FROM BGM_REQUEST_EXPENSE re"
                            + " INNER JOIN BGM_MAS_SOURCE s ON re.BGM_SOURCE_SEQ = s.BGM_SOURCE_SEQ"
                            + " INNER JOIN BGM_MAS_EXPENSE ex ON re.BGM_EXP_SEQ = ex.BGM_EXP_SEQ"
                            + " LEFT JOIN ICM_MAS_RESOURCE_ITEM ri ON re.ICM_RES_ITEM_SEQ = ri.ICM_RES_ITEM_SEQ"
                            + " LEFT JOIN ICM_MAS_UNIT u ON ri.ICM_UNIT_SEQ = u.ICM_UNIT_SEQ"
                        + " WHERE re.BGM_REQ_EXP_SEQ = @seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    RecordInfo infoRecord = new RecordInfo();
                    SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                    ResourceMasterItemInfo infoItem = new ResourceMasterItemInfo();
                    MasterUnitInfo infoUnit = new MasterUnitInfo();
                    BgmExpenseInfo infoExpense = new BgmExpenseInfo();
                    BgmSourceInfo infoSource = new BgmSourceInfo();
                    info.Seq = Convert.ToInt32(dr[0]["BGM_REQ_EXP_SEQ"]);
                    info.Descr = (dr[0]["BGM_REQ_EXP_DESCR"]).ToString();
                    info.Qty = (Convert.IsDBNull(dr[0]["BGM_REQ_EXP_QTY"]) ? null : (Nullable<int>)(dr[0]["BGM_REQ_EXP_QTY"]));
                    info.Price = (Convert.IsDBNull(dr[0]["BGM_REQ_EXP_PRICE"]) ? null : (Nullable<float>)(dr[0]["BGM_REQ_EXP_PRICE"]));
                    infoExpense.Seq = (Convert.IsDBNull(dr[0]["BGM_EXP_SEQ"]) ? null : (Nullable<int>)(dr[0]["BGM_EXP_SEQ"]));
                    infoExpense.Name = (dr[0]["BGM_EXP_NAME"]).ToString();
                    infoSource.Seq = (Convert.IsDBNull(dr[0]["BGM_SOURCE_SEQ"]) ? null : (Nullable<int>)(dr[0]["BGM_SOURCE_SEQ"]));
                    infoSource.Name = (dr[0]["BGM_SOURCE_NAME"]).ToString();
                    infoItem.Seq = (Convert.IsDBNull(dr[0]["ICM_RES_ITEM_SEQ"]) ? null : (Nullable<int>)(dr[0]["ICM_RES_ITEM_SEQ"]));
                    infoItem.Name = (dr[0]["ICM_RES_ITEM_NAME"]).ToString();
                    infoUnit.Name = (dr[0]["ICM_UNIT_NAME"]).ToString();
                    infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                    infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                    infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                    infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));
                    infoStatus.Status = (dr[0]["RECORD_STATUS"]).ToString();
                    infoRecord.InfoStatus = infoStatus;
                    info.InfoRecord = infoRecord;
                    infoItem.InfoUnit = infoUnit;
                    info.InfoItem = infoItem;
                    info.InfoExpense = infoExpense;
                    info.InfoSource = infoSource;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
